import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '状态',		name: 'enabled',		type:'select',	loading:true,	idStr:'code' },
			{ label: '标本架名称',	name: 'sf_name'},
			{ label: '标本架编号',	name: 'sf_number'},
			{ label: '标本架代码',	name: 'sf_code'},
			{ label: '所属分拣组',	name: 'gp_name',			type:'select',	loading:true,	idStr:'uuid',	nameStr: 'gp_name',	onValue:({rows})=>{ this.model.gp_name=rows?.gp_name }},
			{ label: '科室',		name: 'project_name',	type:'select',	loading:true,	idStr:'id',	onValue:({rows})=>{ this.model.project_name=rows?.name }},
		],
		// 提交表单列表
		formData : [
			{ label: '容器名称',		name: 'sf_name', 		half:true, 					required:true},
			{ label: '拼音简称',		name: 'pinyin_short', 	half:true	},
			{ label: '容器类型',		name: 'shelf_type', 	half:true,  type: 'select',	required:true, loading:true,	idStr:'code' },
			{ label: '所属分拣组',	name: 'pick_id', 		half:true,  type: 'select', required:true, loading:true, 	idStr:'uuid',	nameStr: 'gp_name' },
			{ label: '容量',		name: 'total_capacity', half:true, 	type: 'number', required:true },
			{ label: '备注',		name: 'remark', 		full:true, 	type:'textarea' },
		]
	}
	submit = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
	
		$cache.then( async f => {
			// 所属分拣组
			const _data = await f.getCache({ api:'SortGroupSelect' })
			$fn.setRow( forms, 'gp_name', _data )
			$fn.setRow( formData, 'pick_id', _data )
			// 科室
			$fn.setRow( forms, 'project_name', await f.getCache({ api:'shi_yan_ke_shi' }) )
			// 状态
			await $fn.setRow( forms, 'enabled', await f.getDicCache({ code:46100 }) )
			this.setState({ forms, formData })
		})
	}
	
	// 弹窗获取下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 容器类型
			await $fn.setRow( formData, 'shelf_type', await f.getDicCache({ code:20 }) )
			
			this.setState({ formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'shelf/index')
	
	// 表单
	cols = [
		{ title: '标本架编号', 	dataIndex: 'sf_number', 		...$fn.table.spec_code },
		{ title: '标本架名称', 	dataIndex: 'sf_name', 			width:150 },
		{ title: '标本架代码', 	dataIndex: 'sf_code', 			...$fn.table.exp_num },
		{ title: '容器类型', 	dataIndex: 'shelf_type_name', 	width:150 },
		{ title: '所属分拣组', 	dataIndex: 'gp_name', 			width:150 },
		{ title: '容量', 		dataIndex: 'total_capacity', 	...$fn.table.num,	sorter:true },		 /* 标本箱容量增加排序 2021-8-13 --马婷 */
		{ title: '科室', 		dataIndex: 'project_name', 		width:150 },
		{ title: '备注', 		dataIndex: 'remark', 			width:150 },
		{ title: '状态', 		dataIndex: 'enabled_name',  	...$fn.table.status},
		TableControls.controls.call(this,{ width: 180, align:'left' })
	]
	
	// 表格操作按钮
	table_controls = (text,{uuid,enabled}) => {
		return [
			TableControls.edit.call(this, 'shelf/info', { uuid },{   						// 编辑
				select: this.getModalSelect,
				load: ({ id, uuid, sf_number, sf_code, use_capacity, sort, created_at, enabled, comp_id })=> {
					// this.submit = {id, uuid, sf_number, sf_code, use_capacity, sort, created_at, enabled, comp_id}
				}
			}),
			+enabled === 46101 ? Confirm.call(this, '启用', 'shelf/on',  { param: [uuid] }) : null, 				// 启用
			+enabled === 46102 ? Confirm.call(this, '禁用', 'shelf/del', { param: [uuid] }) : null,					// 禁用
			+enabled === 46103 ? Confirm.call(this, '退回', 'shelf/off', { param: [uuid] }) : null,					// 退回
			+enabled === 46102 ? Confirm.call(this, '领用', 'shelf/use', { param: [uuid],idStr:'shelf_id'}) : null,	// 领用
		]
	}
	
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, {click: this.getModalSelect } ),					// 新增
			Confirm.call(this, '启用', 'shelf/on', 	{ isKeys:1 }), 						// 启用
			Confirm.call(this, '禁用', 'shelf/del', { isKeys:1 }),						// 禁用
			Confirm.call(this, '退回', 'shelf/off', { isKeys:1 }),						// 退回
			Confirm.call(this, '领用', 'shelf/use', { idStr:'shelf_id', isKeys:1 }),	// 领用
			Controls.printer.call(this, {label:'批量打印',code: 50107, batch:true,} ) 	// 批量打印
		]
	}
	
	render(){
		return (
			<>
				<Page title='标本架管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { sort:true, isInit: window.isInit } ) }
				</Page>
				{/* 添加 编辑 */}
				{AddEditModal.call(this,{title:'标本架管理', labelWidth:'s6', width:800, api:'shelf/add', editApi:'shelf/edit' })}
			</>
		)
	}
}

export default Index